﻿using System;
using System.Collections.Generic;

namespace AllenalaMente.State
{
    public class GameStateManager
    {
        private static GameState gameState;

        public static GameState GetState()
        {
            if (gameState == null)
                gameState = gameState = SaveUtils.LoadGame();

            if (gameState == null)
                CreateInitialState();

            return gameState;
        }


        public static void ResetGameState()
        {
            gameState.chosenGames[0] = -1;
            gameState.chosenGames[1] = -1;
            gameState.chosenGames[2] = -1;
            gameState.chosenGames[3] = -1;

            gameState.BestScore = 0;
            gameState.lastPubishedScore = 0;
            gameState.Coins = 100; // Credito Neuri

            gameState.GamesList.Clear();

            // Giochi di memoria
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Me_Lost, gameCategory = GameCategories.Memory, isActive = true, iconPath = "Assets/Images/Icons/IcoLost.png", istructions = Locale.Strings.TutorialLost, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoLost.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/LostTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Me_Recall, gameCategory = GameCategories.Memory, isActive = false, iconPath = "Assets/Images/Icons/IcoRecall.png", istructions = Locale.Strings.TutorialRecall, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoRecall.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/RecallTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Memory, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

            // Giochi di matematica
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Ma_Bubbles, gameCategory = GameCategories.Math, isActive = true, iconPath = "Assets/Images/Icons/IcoBubbles.png", istructions = Locale.Strings.TutorialBubbles, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoBubbles.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/BubblesTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Ma_Blocks, gameCategory = GameCategories.Math, isActive = false, iconPath = "Assets/Images/Icons/IcoBlocks.png", istructions = Locale.Strings.TutorialBlocks, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoBlocks.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/BlocksTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Math, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

            // Giochi di riflessi
            //gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Re_Target, gameCategory = GameCategories.Reflex, isActive = true, iconPath = "Assets/Images/Icons/IcoTarget.png", istructions = Locale.Strings.TargetIstructions, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoTarget.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Re_Sword, gameCategory = GameCategories.Reflex, isActive = true, iconPath = "Assets/Images/Icons/IcoSword.png", istructions = Locale.Strings.TutorialSword, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoSword.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/SwordTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Re_Quake, gameCategory = GameCategories.Reflex, isActive = false, iconPath = "Assets/Images/Icons/IcoQuake.png", istructions = Locale.Strings.TutorialQuake, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoQuake.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/QuakeTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Reflex, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

            // Giochi di logica
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Lo_Puzzle, gameCategory = GameCategories.Logic, isActive = true, iconPath = "Assets/Images/Icons/IcoPuzzle.png", istructions = Locale.Strings.TutorialPuzzle, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoPuzzle.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/PuzzleTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Lo_Flux, gameCategory = GameCategories.Logic, isActive = false, iconPath = "Assets/Images/Icons/IcoFlux.png", istructions = Locale.Strings.TutorialFlux, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoFlux.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/FluxTutorail.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Logic, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

        }
        /// <summary>
        /// Crea uno stato iniziale (nuova partita)
        /// </summary>
        /// <returns>Lo stato appena creato</returns>
        public static void CreateInitialState()
        {
            gameState = new GameState();

            gameState.GamesList = new List<GameInfo>();

            // Giochi di memoria
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Me_Lost, gameCategory = GameCategories.Memory, isActive = true, iconPath = "Assets/Images/Icons/IcoLost.png", istructions = Locale.Strings.TutorialLost, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoLost.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/LostTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Me_Recall, gameCategory = GameCategories.Memory, isActive = false, iconPath = "Assets/Images/Icons/IcoRecall.png", istructions = Locale.Strings.TutorialRecall, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoRecall.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/RecallTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Memory, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

            // Giochi di matematica
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Ma_Bubbles, gameCategory = GameCategories.Math, isActive = true, iconPath = "Assets/Images/Icons/IcoBubbles.png", istructions = Locale.Strings.TutorialBubbles, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoBubbles.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/BubblesTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Ma_Blocks, gameCategory = GameCategories.Math, isActive = false, iconPath = "Assets/Images/Icons/IcoBlocks.png", istructions = Locale.Strings.TutorialBlocks, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoBlocks.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/BlocksTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Math, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

            // Giochi di riflessi
            //gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Re_Target, gameCategory = GameCategories.Reflex, isActive = true, iconPath = "Assets/Images/Icons/IcoTarget.png", istructions = Locale.Strings.TargetIstructions, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoTarget.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Re_Sword, gameCategory = GameCategories.Reflex, isActive = true, iconPath = "Assets/Images/Icons/IcoSword.png", istructions = Locale.Strings.TutorialSword, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoSword.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/SwordTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Re_Quake, gameCategory = GameCategories.Reflex, isActive = false, iconPath = "Assets/Images/Icons/IcoQuake.png", istructions = Locale.Strings.TutorialQuake, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoQuake.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/QuakeTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Reflex, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

            // Giochi di logica
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Lo_Puzzle, gameCategory = GameCategories.Logic, isActive = true, iconPath = "Assets/Images/Icons/IcoPuzzle.png", istructions = Locale.Strings.TutorialPuzzle, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoPuzzle.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/PuzzleTutorial.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.Lo_Flux, gameCategory = GameCategories.Logic, isActive = false, iconPath = "Assets/Images/Icons/IcoFlux.png", istructions = Locale.Strings.TutorialFlux, istructionImagePaths = "/AllenalaMente;component/Assets/Images/Icons/IcoFlux.png", tutorialImagePaths = "/AllenalaMente;component/Assets/TutorialImages/FluxTutorail.png" });
            gameState.GamesList.Add(new GameInfo() { gameId = GamesId.random, gameCategory = GameCategories.Logic, isActive = false, iconPath = "Assets/Images/Icons/IcoRandom.png", tutorialImagePaths = "Assets/Images/Icons/IcoRandom.png" });

        }

        /// <summary>
        /// Restituisce l'indice associato all' id del gioco
        /// </summary>
        /// <param name="id">Id del Gioco</param>
        /// <returns>Indice del gioco</returns>
        public static int getIndex(State.GamesId id)
        {
            for (int i = 0; i < gameState.GamesList.Count; i++)
            {
                if (gameState.GamesList[i].gameId == id) return i;
            }

            return -1;
        }

        /// <summary>
        /// Restituisce l'indice associato all' id di un gioco
        /// estratto a caso della categoria indicata
        /// </summary>
        /// <param name="categories">Categoria del gioco da estrarre</param>
        /// <returns>Indice del gioco</returns>
        public static int getRandomIndex(GameCategories categories)
        {
            Random rnd = new Random();

            int index = rnd.Next(gameState.GamesList.Count);

            while (gameState.GamesList[index].gameCategory != categories || gameState.GamesList[index].gameId == GamesId.random)
            {
                index = rnd.Next(gameState.GamesList.Count);
            }

            return index;
        }
    }
}
